package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.Member;
import com.zuipin.entity.MemberAccount;
import com.zuipin.entity.MemberLevel;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.IMemberLevelService;
import com.zuipin.service.IMemberService;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.EnumVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/** 
 * @ClassName: MemberLevelServiceImpl 
 * @author wuyicheng
 * @time 2017年3月27日下午4:02:07 
 */
@Api(value = "会员等级", description = "会员等级-wuyicheng")
@RestController
@RequestMapping(value = "/member/memberLevel")
public class MemberLevelController {
	@Resource
	private IMemberLevelService		memberLevelService;
	@Resource
	private IMemberAccountService	memberAccountService;
	@Resource
	private IMemberService			memberService;
	
	@RequestMapping(value = "/findByMobilePhoneAndSysId", method = RequestMethod.GET)
	@ApiOperation(value = "根据手机号和系统ID查询等级-wuyicheng", notes = "根据手机号和系统ID查询等级-wuyicheng")
	@Valids({ @Valid(name = "mobilePhone", regex = RegexUtils.MOBILE_FUZZY), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "mobilePhone", value = "手机号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<MemberLevel> findByMobilePhoneAndSysId(@RequestParam String mobilePhone, @RequestParam Long sysId) {
		Result<MemberLevel> result = new Result<>();
		Member member = memberService.findByLoginName(mobilePhone);
		if (member != null) {
			MemberAccount memberAccount = memberAccountService.findByMemberIdAndSysId(member.getId(), sysId);
			if (memberAccount != null) {
				MemberLevel memberLevel = memberLevelService.findById(memberAccount.getMemberLevelId());
				result.setData(memberLevel);
				result.setErrorCode(ErrorCode.SUCCESS);
			} else {
				result.setErrorCode(ErrorCode.NOT_FIND);
				result.setMessage("memberAccount is not exist.");
			}
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("member is not exist.");
		}
		return result;
	}
	
	@RequestMapping(value = "/findByMemberIdAndSysId", method = RequestMethod.GET)
	@ApiOperation(value = "根据会员ID和系统ID查询等级-wuyicheng", notes = "根据会员ID和系统ID查询等级-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>2000-会员信息不存在")
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<MemberLevel> findByMemberIdAndSysId(@RequestParam Long memberId, @RequestParam Long sysId) {
		Result<MemberLevel> result = new Result<>();
		MemberAccount memberAccount = memberAccountService.findByMemberIdAndSysId(memberId, sysId);
		if (memberAccount != null) {
			MemberLevel memberLevel = memberLevelService.findById(memberAccount.getMemberLevelId());
			result.setData(memberLevel);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("memberAccount is not exist.");
		}
		return result;
	}
	
	@RequestMapping(value = "/findLevelBySysId", method = RequestMethod.GET)
	@ApiOperation(value = "根据系统ID查询等级-xujinding", notes = "根据系统ID查询等级-xujinding" + "</br>返回码" + "</br>0-成功")
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<List<EnumVo>> findBySysId(@RequestParam Long sysId) {
		Result<List<EnumVo>> result = new Result<>();
		List<EnumVo> memberLevel = memberLevelService.findLevelListEnum(sysId);
		result.setData(memberLevel);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
}
